import org.apache.solr.client.solrj.SolrQuery;  
import org.apache.solr.client.solrj.SolrServer;  
import org.apache.solr.client.solrj.impl.HttpSolrServer;  
import org.apache.solr.client.solrj.response.QueryResponse;  
import org.apache.solr.common.SolrDocument;  
import org.apache.solr.common.SolrDocumentList;;

/**
 * 简单查询
 * 
 * @author maguoqing
 *
 */
public class Test2 {
	public static void main(String[] args) {
		try {
			String serverUrl = (args != null && args.length > 0) ? args[0]  
			        : "http://localhost:8080/solr/collection1";  
			SolrServer solrServer = new HttpSolrServer(serverUrl);  
			  
			//读取输入参数作为查询关键字，若无关键字，则查询全部内容。  
			String queryString = (args != null && args.length > 1) ? args[1] : "url:163";  
			SolrQuery solrQuery = new SolrQuery(queryString);  
			solrQuery.setRows(5);  
			QueryResponse resp = solrServer.query(solrQuery);  
			SolrDocumentList hits = resp.getResults();  
			  
			for(SolrDocument doc : hits ){  
			    System.out.println(doc.getFieldValue("id").toString() + " : " + doc.getFieldValue("url"));  
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		
	}
}
